package org.gary.product.service.impl;

import static lengreen.util.StringUtils.*;

import java.util.List;

import javax.annotation.Resource;

import org.gary.base.model.Image;
import org.gary.base.service.ImageService;
import org.gary.core.factories.impl.BasicServiceFactoryImpl;
import org.gary.core.util.ResultMessage;
import org.gary.product.dao.ProductDao;
import org.gary.product.model.Product;
import org.gary.product.service.ProductService;
import org.springframework.stereotype.Component;

/**
 * 产品Service类
 * @author lengreen
 * 
 */
@Component("productService")
public class ProductServiceImpl extends BasicServiceFactoryImpl<Product>
		implements ProductService {
	/**
	 * 用于图片日志记录
	 */
	private ImageService imageService;
	private ProductDao productDao;

	/**
	 * 重写basicServicefactoryimpl中的Save方法， 重写以加入日志模块，并记录日志
	 */
	@Override
	public String save(Product product) {
		if (isNull(product.getName())) {
			return ResultMessage.product.ADD_NONE;
		} else {
			for (Image image : product.getImages()) {
				imageService.save(image);
			}
			// 在商品保存之前记录一条日志
			getLoggerService().makeLog(product.getClass().getName(),
					"开始保存一个商品，" + product);
			getBasicDaoFactory().save(product);
			// 在商品保存之后记录一条日志
			getLoggerService().makeLog(product.getClass().getName(),
					"保存一个商品成功，" + product);
			return ResultMessage.product.ADD_SUCCESS;
		}
	}

	@Override
	public List<Image> loadImages(int productId) {
		return productDao.loadImages(productId);
	}
	
	@Resource
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public ImageService getImageService() {
		return imageService;
	}

	@Resource
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	@Override
	public List<Product> getProductList() {
		return list(Product.class);
	}

	@Override
	public List<Product> getProductList(int top,int page,Object lang,int type) {
		return list(Product.class, top, page, lang, type);
	}
}
